﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Topro.Application.Impl
{
    using Dapper;

    using Topro.Application.Interface;
    using Topro.Db;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Response.DeviceData;
    using Topro.Log;

    /// <summary>
    /// 设备数据接口实现
    /// </summary>
    public class DeviceDataServiceImpl: DeviceDataService
    {
        #region 注入
        /// <summary>
        /// db
        /// </summary>
        private readonly IDb _db;

        /// <summary>
        /// loggerService
        /// </summary>
        private readonly LoggerService _loggerService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dapperFactory"></param>
        /// <param name="logger"></param>
        public DeviceDataServiceImpl(IDb db, LoggerService loggerService)
        {
            _db = db;
            _loggerService = loggerService;
        }
        #endregion

        #region 设备数据查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedList<DeviceDataQueryResponse> GetDeviceDataPager(DeviceDataQuery query)
        {
            var pageIndex = query.PageIndex <= 0 ? 0 : (query.PageIndex - 1) * query.PageSize;

            var files = "dd.Id,dd.DeviceLine,dd.PlcAddr,dd.[Index],dd.Message,dd.StartTime,dd.EndTime,dd.Status,dd.CreatedDate";
            var tableName = "DeviceData dd";
            var orderby = "ORDER BY dd.Id DESC";
            var where = "where 1=1";

            #region 条件
            if (query.DeviceLine.HasValue)
            {
                where += string.Format(" And dd.DeviceLine ={0}", query.DeviceLine.Value);
            }

            if (query.Status.HasValue)
            {
                where += string.Format(" And dd.Status ={0}", query.Status.Value);
            }

            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                where += string.Format(" And dd.CreatedDate between '{0}' and '{1}'", query.StartTime, query.EndTime);
            }
            #endregion

            var sb = new StringBuilder();

            sb.AppendFormat("SELECT COUNT(1) FROM {0} {1};", tableName, where);
            sb.AppendFormat("select {0} from {1} {2} {3} offset {4} rows fetch next {5} rows only", files, tableName, where, orderby, pageIndex, query.PageSize);

            using (var multi = _db.DbConnection.QueryMultiple(sb.ToString()))
            {
                var count = multi.ReadFirst<int>();

                var list = multi.Read<DeviceDataQueryResponse>();

                return new PagedList<DeviceDataQueryResponse>()
                {
                    TData = list,
                    CurrentPageIndex = query.PageIndex,
                    TotalCount = count
                };
            }
        }
        #endregion

        #region 设备数据导出
        /// <summary>
        /// 设备数据导出
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public IEnumerable<DeviceDataQueryResponse> GetDeviceDataExport(DeviceDataQuery query)
        {
            var files = "dd.Id,dd.DeviceLine,dd.PlcAddr,dd.[Index],dd.Message,dd.StartTime,dd.EndTime,dd.Status,dd.CreatedDate";
            var tableName = "DeviceData dd";
            var orderby = "ORDER BY dd.Id DESC";
            var where = "where 1=1";

            #region 条件
            if (query.DeviceLine.HasValue)
            {
                where += string.Format(" And dd.DeviceLine ={0}", query.DeviceLine.Value);
            }

            if (query.Status.HasValue)
            {
                where += string.Format(" And dd.Status ={0}", query.Status.Value);
            }

            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                where += string.Format(" And dd.CreatedDate between '{0}' and '{1}'", query.StartTime, query.EndTime);
            }
            #endregion

            var sb = new StringBuilder();

            sb.AppendFormat("select {0} from {1} {2} {3}", files, tableName, where, orderby);

            using (var multi = _db.DbConnection.QueryMultiple(sb.ToString()))
            {
                var list = multi.Read<DeviceDataQueryResponse>();

                return list;
            }
        }
        #endregion

        #region 叠状图分析
        /// <summary>
        /// 搜索时间叠状图分析
        /// </summary>
        /// <param name="query">参数</param>
        /// <returns></returns>
        public JResult<TimeAnalysisResponse> TimeAnalysis(TimeAnalysisQuery query)
        {
            //输出
            var response = new JResult<TimeAnalysisResponse>()
            {
                Success = false,
                Data = new TimeAnalysisResponse()
            };

            try
            {
                //数据
                var data = new TimeAnalysisResponse()
                {
                    Categories = new List<string>(),
                    TimeAnalysisSeries = new List<TimeAnalysisSeries>(),
                    DayList = new List<DayList>()
                };

                #region 系数
                //设备状态[1:工作 2:空闲 3:异常 4:换料 5:班保养 6:周保养 7：堵满本 8：待空底板 9：换钢板 10：换铜箔 11：换PP,12 无板生产,13 禁止生产,14 计划停机 ]
                //类型
                var categories = new List<string>();
                //系数列表
                var timeAnalysisSeries = new List<TimeAnalysisSeries>();
                //
                var dayList = new List<DayList>();
                //查询语句
                var querySql = "SELECT da.DeviceLine,DATEDIFF(second,da.StartTime,da.EndTime) AS [Second],da.Status,da.CreatedDate FROM DeviceData da WHERE da.DeviceLine=@DeviceLine AND da.CreatedDate BETWEEN @startTime AND @endTime";
                //倍数
                //var diploid = new decimal(4.17);

                #region 类型
                //running(工作)
                var running = new TimeAnalysisSeries()
                {
                    Name = "Running",
                    Color = "#87D300",
                    Data = new List<decimal>()
                };
                //setup(换料)
                var setup = new TimeAnalysisSeries()
                {
                    Name = "Set up",
                    Color = "#91CC75",
                    Data = new List<decimal>()
                };
                //bookblock(堵满本)
                var bookblock = new TimeAnalysisSeries()
                {
                    Name = "Book Block",
                    Color = "#005D26",
                    Data = new List<decimal>()
                };
                //waitingplaten(待空底板)
                var waitingplaten = new TimeAnalysisSeries()
                {
                    Name = "Waiting Platen",
                    Color = "#7A834E",
                    Data = new List<decimal>()
                };
                //SP changing(换钢板)
                var spchanging = new TimeAnalysisSeries()
                {
                    Name = "SP changing",
                    Color = "#31341F",
                    Data = new List<decimal>()
                };
                //copper foil changing(换铜箔)
                var copperfoilchanging = new TimeAnalysisSeries()
                {
                    Name = "Copper foil changing",
                    Color = "#8A7644",
                    Data = new List<decimal>()
                };
                //pp changing(换pp)
                var ppchanging = new TimeAnalysisSeries()
                {
                    Name = "PP changing",
                    Color = "#383222",
                    Data = new List<decimal>()
                };
                //idle(空闲)
                var idle = new TimeAnalysisSeries()
                {
                    Name = "Idle",
                    Color = "#33B56B",
                    Data = new List<decimal>()
                };
                //exception(异常)
                var malfunction = new TimeAnalysisSeries()
                {
                    Name = "Malfunction",
                    Color = "#EE6666",
                    Data = new List<decimal>()
                };
                //Shift service(班保养)
                var shiftservice = new TimeAnalysisSeries()
                {
                    Name = "Shift service",
                    Color = "#4ADE86",
                    Data = new List<decimal>()
                };
                //Weekly service(周保养)
                var weeklyservice = new TimeAnalysisSeries()
                {
                    Name = "Weekly service",
                    Color = "#009531",
                    Data = new List<decimal>()
                };

                //noWIP(无板生产)
                var noWIP = new TimeAnalysisSeries()
                {
                    Name = "No wip",
                    Color = "#51B2C5",
                    Data = new List<decimal>()
                };
                //Quality blok(禁止生产)
                var qualityBlok = new TimeAnalysisSeries()
                {
                    Name = "Quality blok",
                    Color = "#072348",
                    Data = new List<decimal>()
                };
                //Line plan down(计划停机)
                var lineplandown = new TimeAnalysisSeries()
                {
                    Name = "Line plan down",
                    Color = "#17527A",
                    Data = new List<decimal>()
                };
                //Machine Maintenance(设备维修)
                var machinemaintenance = new TimeAnalysisSeries()
                {
                    Name = "Machine Maintenance",
                    Color = "#F2AA31",
                    Data = new List<decimal>()
                };
                #endregion

                #region 循环添加数据
                foreach (var day in query.Days)
                {
                    //开始时间
                    var startTime = day.ToString($"yyyy-MM-dd 00:00:00");
                    //结束时间
                    var endTime = day.ToString("yyyy-MM-dd 23:59:59");
                    //当前时间
                    var dt = DateTime.Now;
                    //获取当天的所有记录
                    var list = _db.DbConnection.Query<ReportQueryResponse>(querySql, new { DeviceLine = 20, startTime = startTime, endTime = endTime });
                    if (list != null && list.Any())
                    {
                        //总秒数
                        var totalSecond = list.Sum(c => c.Second);
                        //var radix = GetHour(list);
                        //if(radix == 0)
                        //{
                        //    radix = 1;
                        //}
                        //ts
                        var tss = new TimeSpan(0, 0, totalSecond);
                        //
                        var radix = tss.Hours == 0 ? 24 : tss.Hours;
                        //倍数new decimal(100 / radix)
                        var diploid = Convert.ToDecimal((new decimal(100) / radix).ToString("#0.00"));
                        //
                        categories.Add(day.Day.ToString("00"));
                        //数据
                        var dayData = new DayData();
                        //列表
                        //var dayList = new List<DayList>();
                        //天
                        dayList.Add(new DayList()
                        {
                            Id = 0,
                            Day = day.Day.ToString("00")
                        });

                        #region //工作
                        var runningSecond = list.Where(c => c.Status == (int)DeviceDataStatus.工作).Sum(c => c.Second);
                        if (runningSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, runningSecond);
                            //取模运算
                            //var mold = ts.Minutes % 2;
                            //
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //分
                            //minutes = minutes + ts.Minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Running",
                                StyeName = "running",
                                Time = hour.ToString("#0.00")
                            });
                            running.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Running",
                                StyeName = "running",
                                Time = "0"
                            });

                            running.Data.Add(0);
                        }
                        #endregion

                        #region //换料
                        var setupSecond = list.Where(c => c.Status == (int)DeviceDataStatus.换料).Sum(c => c.Second);
                        if (setupSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, setupSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////分
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Set up",
                                StyeName = "setup",
                                Time = hour.ToString("#0.00")
                            });
                            setup.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Set up",
                                StyeName = "setup",
                                Time = "0"
                            });

                            setup.Data.Add(0);
                        }
                        #endregion

                        #region //堵满本
                        //bookblock
                        var bookblockSecond = list.Where(c => c.Status == (int)DeviceDataStatus.堵满本).Sum(c => c.Second);
                        if (bookblockSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, bookblockSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Book block",
                                StyeName = "bookblock",
                                Time = hour.ToString("#0.00")
                            });
                            bookblock.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Book block",
                                StyeName = "bookblock",
                                Time = "0"
                            });
                            bookblock.Data.Add(0);
                        }
                        #endregion

                        #region //待空底板
                        var waitingplatenSecond = list.Where(c => c.Status == (int)DeviceDataStatus.待空底板).Sum(c => c.Second);
                        if (waitingplatenSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, waitingplatenSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Waiting platen",
                                StyeName = "waitingplaten",
                                Time = hour.ToString("#0.00")
                            });

                            waitingplaten.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Waiting platen",
                                StyeName = "waitingplaten",
                                Time = "0"
                            });
                            waitingplaten.Data.Add(0);
                        }
                        #endregion

                        #region //换钢板
                        var spchangingSecond = list.Where(c => c.Status == (int)DeviceDataStatus.换钢板).Sum(c => c.Second);
                        if (spchangingSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, spchangingSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Days>=1? 24: ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "SP changing",
                                StyeName = "spchanging",
                                Time = hour.ToString("#0.00")
                            });

                            spchanging.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "SP changing",
                                StyeName = "spchanging",
                                Time = "0"
                            });
                            spchanging.Data.Add(0);
                        }
                        #endregion

                        #region //换铜箔
                        var cuFoilchangingSecond = list.Where(c => c.Status == (int)DeviceDataStatus.换铜箔).Sum(c => c.Second);
                        if (cuFoilchangingSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, cuFoilchangingSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Copper foil changing",
                                StyeName = "copperfoilchanging",
                                Time = hour.ToString("#0.00")
                            });

                            copperfoilchanging.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Copper foil changing",
                                StyeName = "copperfoilchanging",
                                Time = "0"
                            });

                            copperfoilchanging.Data.Add(0);
                        }
                        #endregion

                        #region //换PP
                        var ppchangingSecond = list.Where(c => c.Status == (int)DeviceDataStatus.换PP).Sum(c => c.Second);
                        if (ppchangingSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, ppchangingSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "PP changing",
                                StyeName = "ppchanging",
                                Time = hour.ToString("#0.00")
                            });

                            ppchanging.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "PP changing",
                                StyeName = "ppchanging",
                                Time = "0"
                            });

                            ppchanging.Data.Add(0);
                        }
                        #endregion

                        #region //空闲
                        var idleSecond = list.Where(c => c.Status == (int)DeviceDataStatus.空闲).Sum(c => c.Second);
                        if (idleSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, idleSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Idle",
                                StyeName = "idle",
                                Time = hour.ToString("#0.00")
                            });

                            idle.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Idle",
                                StyeName = "idle",
                                Time = "0"
                            });

                            idle.Data.Add(0);
                        }
                        #endregion

                        #region //异常
                        var malfunctionSecond = list.Where(c => c.Status == (int)DeviceDataStatus.异常).Sum(c => c.Second);
                        if (malfunctionSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, malfunctionSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Malfunction",
                                StyeName = "malfunction",
                                Time = hour.ToString("#0.00")
                            });

                            malfunction.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Malfunction",
                                StyeName = "malfunction",
                                Time = "0"
                            });

                            malfunction.Data.Add(0);
                        }
                        #endregion

                        #region //班保养
                        var shiftserviceSecond = list.Where(c => c.Status == (int)DeviceDataStatus.班保养).Sum(c => c.Second);
                        if (shiftserviceSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, shiftserviceSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Shift service",
                                StyeName = "shiftservice",
                                Time = hour.ToString("#0.00")
                            });

                            shiftservice.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Shift service",
                                StyeName = "shiftservice",
                                Time = "0"
                            });

                            shiftservice.Data.Add(0);
                        }
                        #endregion

                        #region //周保养
                        var weeklyserviceSecond = list.Where(c => c.Status == (int)DeviceDataStatus.周保养).Sum(c => c.Second);
                        if (weeklyserviceSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, weeklyserviceSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Weekly service",
                                StyeName = "weeklyservice",
                                Time = hour.ToString("#0.00")
                            });

                            weeklyservice.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Weekly service",
                                StyeName = "weeklyservice",
                                Time = "0"
                            });

                            weeklyservice.Data.Add(0);
                        }
                        #endregion

                        #region //无板生产
                        var noWIPSecond = list.Where(c => c.Status == (int)DeviceDataStatus.无板生产).Sum(c => c.Second);
                        if (noWIPSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, noWIPSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "No wip",
                                StyeName = "nowip",
                                Time = hour.ToString("#0.00")
                            });

                            noWIP.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "No WIP",
                                StyeName = "nowip",
                                Time = "0"
                            });

                            noWIP.Data.Add(0);
                        }
                        #endregion

                        #region //禁止生产
                        var qualityBlokSecond = list.Where(c => c.Status == (int)DeviceDataStatus.禁止生产).Sum(c => c.Second);
                        if (qualityBlokSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, qualityBlokSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Quality blok",
                                StyeName = "qualityblok",
                                Time = hour.ToString("#0.00")
                            });

                            qualityBlok.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Quality Blok",
                                StyeName = "qualityblok",
                                Time = "0"
                            });

                            qualityBlok.Data.Add(0);
                        }
                        #endregion

                        #region //计划停机
                        var planLinedownSecond = list.Where(c => c.Status == (int)DeviceDataStatus.计划停机).Sum(c => c.Second);
                        if (planLinedownSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, planLinedownSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Plan line down",
                                StyeName = "planlinedown",
                                Time = hour.ToString("#0.00")
                            });

                            lineplandown.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Plan line down",
                                StyeName = "planlinedown",
                                Time = "0"
                            });

                            lineplandown.Data.Add(0);
                        }
                        #endregion

                        #region //设备维修
                        var machinemaintenanceSecond = list.Where(c => c.Status == (int)DeviceDataStatus.设备维修).Sum(c => c.Second);
                        if (machinemaintenanceSecond > 0)
                        {
                            var ts = new TimeSpan(0, 0, machinemaintenanceSecond);
                            ////取模运算
                            //var mold = ts.Minutes % 2;
                            ////
                            //var minutes = mold == 0 ? ts.Minutes + 1 : ts.Minutes;
                            ////时
                            //var hour = ts.Hours + Convert.ToDecimal(minutes) / 100;
                            //分
                            var minutes = Convert.ToDecimal(Convert.ToDecimal(ts.Minutes) / 60);
                            //时
                            var hour = ts.Hours + minutes;
                            //百分比
                            var percent = Convert.ToDecimal((hour * diploid).ToString("#0.00"));
                            //
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Machine Maintenance",
                                StyeName = "machinemaintenance",
                                Time = hour.ToString("#0.00")
                            });

                            machinemaintenance.Data.Add(percent);
                        }
                        else
                        {
                            dayList.Add(new DayList()
                            {
                                Id = 1,
                                Name = "Machine Maintenance",
                                StyeName = "machinemaintenance",
                                Time = "0"
                            });

                            machinemaintenance.Data.Add(0);
                        }
                        #endregion

                        #region 数据
                        //dayData.DayList = dayList;
                        #endregion
                    }
                }
                #endregion

                //running
                timeAnalysisSeries.Add(running);
                //setup
                timeAnalysisSeries.Add(setup);
                //book block
                timeAnalysisSeries.Add(bookblock);
                //waiting platen
                timeAnalysisSeries.Add(waitingplaten);
                //spchanging
                timeAnalysisSeries.Add(spchanging);
                //cufoilchanging
                timeAnalysisSeries.Add(copperfoilchanging);
                //ppchanging
                timeAnalysisSeries.Add(ppchanging);
                //idle
                timeAnalysisSeries.Add(idle);
                //exception
                timeAnalysisSeries.Add(malfunction);
                //Shift service
                timeAnalysisSeries.Add(shiftservice);
                //Weekly service
                timeAnalysisSeries.Add(weeklyservice);
                //noWIP
                timeAnalysisSeries.Add(noWIP);
                //qualityBlok
                timeAnalysisSeries.Add(qualityBlok);
                //lineplandown
                timeAnalysisSeries.Add(lineplandown);
                //machinemaintenance
                timeAnalysisSeries.Add(machinemaintenance);

                data.Categories = categories;
                data.TimeAnalysisSeries = timeAnalysisSeries;
                data.DayList = dayList;
                #endregion

                response.Success = true;
                response.Data = data;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return response;
        }
        #endregion

        #region 计算当前小时
        /// <summary>
        /// 计算当前小时
        /// </summary>
        /// <returns></returns>
        public int GetHour(List<ReportQueryResponse> list)
        {
            var resultHour = 0;

            if (list != null && list.Any())
            {
                #region //工作
                var workingSecond = list.Where(c => c.Status == (int)DeviceDataStatus.工作).Sum(c => c.Second);
                if (workingSecond > 0)
                {
                    var ts = new TimeSpan(0, 0, workingSecond);
                    if (ts.Hours > 0)
                    {
                        resultHour += ts.Hours;
                    }
                }

                #endregion

                #region //准备
                //准备4,7,8,9,10,11
                var readyStatus = new List<int>()
                        {
                            (int)DeviceDataStatus.换料,
                            (int)DeviceDataStatus.堵满本,
                            (int)DeviceDataStatus.待空底板,
                            (int)DeviceDataStatus.换钢板,
                            (int)DeviceDataStatus.换铜箔,
                            (int)DeviceDataStatus.换PP
                        };

                var readySecond = list.Where(c => readyStatus.Contains(c.Status)).Sum(c => c.Second);
                if (readySecond > 0)
                {
                    var ts = new TimeSpan(0, 0, readySecond);
                    if (ts.Hours > 0)
                    {
                        resultHour += ts.Hours;
                    }
                }
                #endregion

                #region //空闲
                var idleSecond = list.Where(c => c.Status == (int)DeviceDataStatus.空闲).Sum(c => c.Second);
                if (idleSecond > 0)
                {
                    var ts = new TimeSpan(0, 0, idleSecond);
                    if (ts.Hours > 0)
                    {
                        resultHour += ts.Hours;
                    }
                }
                #endregion

                #region //异常
                var exceptionSecond = list.Where(c => c.Status == (int)DeviceDataStatus.异常).Sum(c => c.Second);
                if (exceptionSecond > 0)
                {
                    var ts = new TimeSpan(0, 0, exceptionSecond);
                    if (ts.Hours > 0)
                    {
                        resultHour += ts.Hours;
                    }
                }
                #endregion

                #region //维护
                var serviceStatus = new List<int>()
                        {
                            (int)DeviceDataStatus.班保养,
                            (int)DeviceDataStatus.周保养
                        };

                var serviceSecond = list.Where(c => serviceStatus.Contains(c.Status)).Sum(c => c.Second);
                if (serviceSecond > 0)
                {
                    var ts = new TimeSpan(0, 0, serviceSecond);
                    if (ts.Hours > 0)
                    {
                        resultHour += ts.Hours;
                    }
                }
                #endregion
            }

            return resultHour;
        }
        #endregion
    }
}
